<?php

include '../../auth/ceklogin.php';
include "../../sql/conn.php";
$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
$offset = ($page - 1) * $rows;
$result = array();

$where = " po.idjob is not null ";
$jobno = '';
$pospkno = '';
$idcustomer = '';
$jobstatus = '';

if (isset($_POST['ref'])) {
    $ref = explode('&', $_POST['ref']);

    //job no
    $jobno = explode('=', $ref[0]);
    $where .= isset($jobno[1]) ? " and po.no_job like '%" . $jobno[1] . "%' " : "";

    //pospkno
    $pospkno = explode('=', $ref[1]);
    $where .= isset($pospkno[1]) ? " and po.no_po_in like '%" . $pospkno[1] . "%'" : "";

    //id customer
    $idcustomer = explode('=', $ref[2]);
    $where .= isset($idcustomer[1]) ? " and po.idcustomer like '%" . $idcustomer[1] . "%'" : "";

    //job status
    $jobstatus = explode('=', $ref[3]);
    $where .= isset($jobstatus[1]) ? " and po.status_job like '%" . $jobstatus[1] . "%'" : "";
}

$rs = mysql_query("
    SELECT COUNT(*) 
    FROM job po
    where $where
    ");
$row = mysql_fetch_row($rs);
$result["total"] = $row[0];
$rs = mysql_query("
        select 
            po.idJob
            ,po.no_job
            ,po.no_po_in
            ,po.idCustomer
            ,po.desc_job
            ,format(po.dpp_job,0) as dpp_job
            ,case po.status_job
            	when 'H' then 'Hold'
            	when 'P' then 'Pending'
            	when 'D' then 'Done'
            end as status_job
            ,po.inputby_job
            ,po.inputdate_job
            ,format(po.discount_job,0) as discount_job
            ,c.idCustomer
            ,c.namaCustomer
            ,CONCAT ('<a href=\"javascript:void(0)\" onclick=\"getDetailJob(',po.idJob,')\" style=\"color:red\">Get Detail</a>') as detail 
        from 
            job po
            ,customer c 
        where $where 
            and c.idCustomer=po.idCustomer
        order by 
            po.inputdate_job desc
        limit $offset,$rows
    ");
$rows = array();
while ($row = mysql_fetch_object($rs)) {
    array_push($rows, $row);
}
$result["rows"] = $rows;
echo json_encode($result);
?>
